<!--
  This file is a part of the open-eBackup project.
  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
  If a copy of the MPL was not distributed with this file, You can obtain one at
  http://mozilla.org/MPL/2.0/.
  
  Copyright (c) [2024] Huawei Technologies Co.,Ltd.
  
  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  -->

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en-us" xml:lang="en-us">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="DC.Type" content="topic">
  <meta name="DC.Title" content="Pod Configuration in the Production Environment for Application-Consistent Backup (MairaDB as the Containerized Application)">
  <meta name="product" content="">
  <meta name="DC.Relation" scheme="URI" content="en-us_topic_0000002164749778.html">
  <meta name="prodname" content="">
  <meta name="version" content="">
  <meta name="brand" content="">
  <meta name="DC.Publisher" content="20250306">
  <meta name="prodname" content="csbs">
  <meta name="documenttype" content="usermanual">
  <meta name="DC.Format" content="XHTML">
  <meta name="DC.Identifier" content="EN-US_TOPIC_0000002236642493">
  <meta name="DC.Language" content="en-us">
  <link rel="stylesheet" type="text/css" href="public_sys-resources/commonltr.css">
  <title>Pod Configuration in the Production Environment for Application-Consistent Backup (MairaDB as the Containerized Application)</title>
 </head>
 <body style="clear:both; padding-left:10px; padding-top:5px; padding-right:5px; padding-bottom:5px">
  <a name="EN-US_TOPIC_0000002236642493"></a><a name="EN-US_TOPIC_0000002236642493"></a>
  <h1 class="topictitle1">Pod Configuration in the Production Environment for Application-Consistent Backup (MairaDB as the Containerized Application)</h1>
  <div>
   <p>If there are multiple pre- or post-processing commands for creating a snapshot for a PVC, you can write multiple pre-executed commands to the same script and save the script to the persistent directory of a container. Running the script to execute multiple commands simplifies the command input process.</p>
   <p>The following are examples of the pre- and post-processing commands:</p>
   <pre class="screen">#Pre-processing command script
#!/bin/bash
#Run the following command to connect to the database. In the command, <strong>root</strong> is the username and <strong>huawei@123</strong> is the database password. Replace them with the actual ones.
if mariadb -uroot -phuawei@123 -e "SELECT 1;" 2&gt;/dev/null; then
#If the MairaDB database is connected, run the following command to lock tables. If the table locking command is executed successfully, <strong>200</strong> is displayed in the echo command output. The system invokes the <strong>sleep</strong> command to lock the MairaDB database.
#Note: The lock duration of the MairaDB database is longer than the backup duration.
    mariadb -uroot -phuawei@123 -e "flush tables with read lock;system echo "200";system sleep 3000;"
else
#Failed to connect to the database.
    echo "0"
fi</pre>
   <pre class="screen">#Post-processing command script
#!/bin/bash
#If you need to stop execution of the pre-processing script in advance, forcibly stop the sleep process in the pre-processing script. In the command, set grep based on the execution result of <strong>ps -ef</strong> in the actual environment, and change the field printed in <strong>awk</strong> based on the <strong>PID</strong> column in the execution result of <strong>ps -ef</strong>.
pid=$(ps -ef | grep "/bin/sh -c /var/lib/mariadb/backup/dataprotect_mysql_consistency_pre_script.sh" | grep -v grep | awk '{print $2}')
#Note: If the process ID of the pre-processing script does not exist in the system, execution of the pre-processing script has ended before the post-processing script is invoked to unlock the MairaDB database. In this case, the backup data may be inconsistent. As a result, the post-processing script fails to be executed.
[ ! $pid ] &amp;&amp; echo "pid is null" &amp;&amp; echo "0" &amp;&amp; exit
#Forcibly stop the sleep process in the pre-processing script to unlock the MairaDB database.
subtree=$(pstree -p $pid) subtree=$(pstree -p $pid)  
right_str=${subtree##*\(}  
sleep_pid=${right_str%*)}  
kill -9 $sleep_pid
kill -9 $pid
#The database is unlocked successfully.
echo "200"</pre>
   <div class="section">
    <h4 class="sectiontitle">Procedure</h4>
    <ol>
     <li><span>Upload the scripts to any persistent directory of the container. Run the following commands on the container page to add permissions to the script files:</span><p></p><pre class="screen">chmod +x <em>/var/lib/mariadb/backup/dataprotect_mysql_consistency_pre_script.sh</em>
chmod +x <em>/var/lib/mariadb/backup/dataprotect_mysql_consistency_post_script.sh</em></pre> <p></p></li>
     <li><span>Run the following command to log in to the MariaDB database:</span><p></p><pre class="screen">mariadb -u<em>root</em> -p<em>huawei@123</em></pre> <p><strong>root</strong> indicates the username, and <strong>huawei@123</strong> indicates the database password. Replace them with the actual ones. If the following information is displayed, the database is successfully accessed.</p> <p><span><img src="en-us_image_0000002237508157.png"></span></p> <p></p></li>
     <li><span>Modify all related contents in the scripts and save the settings.</span><p></p>
      <ul>
       <li>Replace <strong>postgres</strong> in the command with the actual password of the user.<pre class="screen">export PGPASSWORD='<em>postgres</em>'</pre></li>
       <li>In the following operations, the pre-processing script location <strong>/var/lib/mariadb/backup/dataprotect_mysql_consistency_pre_script.sh</strong> and post-processing script location <strong>/var/lib/mariadb/backup/dataprotect_mysql_consistency_post_script.sh</strong> are used as examples. You can modify the content based on the actual script locations.</li>
      </ul> <p></p></li>
     <li><span>Add pod labels and annotations by referring to <a href="en-us_topic_0000002164589986.html">Pod Configuration in the Production Environment for Application-Consistent Backup (General)</a>. The following shows examples of the pre- and post-processing commands entered when you add annotations:</span><p></p><pre class="screen">#Pre-processing command
kubectl annotate pod -l <strong><em>label=value </em></strong>-n <strong><em>namespace</em></strong> pre.hook.dataprotect.backup.io/command='["/bin/sh", "-c","<em>/var/lib/mariadb/backup/dataprotect_pgsql_consistency_pre_script.sh</em>"]'
#Post-processing command
kubectl annotate pod -l <strong><em>label=value</em></strong><em> </em>-n <strong><em>namespace</em></strong> post.hook.dataprotect.backup.io/command='["/bin/sh", "-c","<em>/var/lib/mariadb/backup/dataprotect_pgsql_consistency_post_script.sh</em>"]'</pre>
      <div class="note">
       <img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span>
       <div class="notebody">
        <p>To ensure that the pre- and post-processing commands can be executed properly, the script locations in the commands must be the same as the actual locations to which the scripts are uploaded.</p>
       </div>
      </div> <p></p></li>
    </ol>
   </div>
  </div>
  <div>
   <div class="familylinks">
    <div class="parentlink">
     <strong>Parent topic:</strong> <a href="en-us_topic_0000002164749778.html">FAQs</a>
    </div>
   </div>
  </div>
 </body>
</html>